/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mbox.server;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import mbox.helper.Utilities;

/**
 *
 * @author Hallucinogen
 */
/*
public class ServerHandler implements HttpHandler{

    public ServerHandler(Server server) {
        mServer = server;
    }

    public void handle(HttpExchange he) throws IOException {
        if (he.getRequestMethod().equalsIgnoreCase("post")) {
            doPost(he);
        }
        else if (he.getRequestMethod().equalsIgnoreCase("get")) {
            doGet(he);
        }

        he.close();
    }

    private void doGet(HttpExchange he) throws IOException {
        
    }

    private void doPost(HttpExchange he) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(he.getRequestBody());

        StringBuilder sb = new StringBuilder();
        int r;
        while ((r = bis.read()) != -1){
            sb.append((char)r);
        }

        String query = sb.toString();

        String username = Utilities.getValueFromHTTPGetURL(query, "username");
        String password = Utilities.getValueFromHTTPGetURL(query, "password");
        String fileType = null;
        String path     = null;
        String method   = Utilities.getValueFromHTTPGetURL(query, "method");
        
        OutputStream os = he.getResponseBody();
        //if (!validateUser(username, password)) return;
        if (method.equalsIgnoreCase("metadata")) {
            sb = new StringBuilder();

            File file = new File("repo/" + username + "/");
            ArrayList<String> metadata = new ArrayList<String>();
            Utilities.getMetaDataAllFile(file, "", metadata);

            sb.append(metadata.size());
            sb.append("\n");
            for (int i = 0; i < metadata.size(); ++i) {
                sb.append(metadata.get(i));
                sb.append("\n");
            }

            String result = sb.toString();
            he.sendResponseHeaders(HttpURLConnection.HTTP_OK, result.length());

            for (int i = 0; i < result.length(); ++i)
                os.write((int)result.charAt(i));
            os.flush();
        } else if (method.equalsIgnoreCase("download")) {
            /// TODO : get file from server (give file to client)
            fileType    = (String)he.getAttribute("file_type");
            path        = (String)he.getAttribute("path");
            
        } else if (method.equalsIgnoreCase("upload")) {
            /// TODO : upload file to server
            fileType = (String)he.getAttribute("file_type");
            path        = (String)he.getAttribute("path");
        }

        os.close();
    }

    private boolean validateUser(String username, String password) {
        Connection con = null;

        try{
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(ServerHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ServerHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (ClassNotFoundException ex){
        }

        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/progin5?user=root&password=");
            Statement stm = con.createStatement();
            ResultSet rst = null;
            /// TODO : synchronize with Fikry's database
            if (stm.execute("select * from user where username ='" + username + "' AND password = '"+ password +"'"))
            {
                rst = stm.getResultSet();
            }
            if (rst != null) {
                return true;
            }
        } catch (SQLException ex) {
            
        }

        return false;
    }

    

    private Server mServer;
}
*/